//
//  HTCustomLayout.m
//  HeartTravel
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "HTCustomLayout.h"

@interface HTCustomLayout ()

@property (nonatomic, strong) NSMutableArray *frameArr; // 存放每一列的frame
@property (nonatomic, strong) NSMutableArray *attributesArr; // 存放每一个item的属性数值

@end

@implementation HTCustomLayout
// 实现里可以定义实例变量,但不能定义属性
{
    NSUInteger _numberOfColumns; // 一共有多少列
}

- (NSMutableArray *)frameArr {
    
    if (_frameArr == nil) {
        self.frameArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _frameArr;
}

- (NSMutableArray *)attributesArr {
    
    if (_attributesArr == nil) {
        self.attributesArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _attributesArr;
}

- (instancetype)initWithNumberOfColumns:(NSUInteger)columns {
    
    self = [super init];
    if (self) {
        _numberOfColumns = columns;
    }
    return self;
}
#warning ___+_+_+_+_+_++_+_+
- (void)prepareLayout {
    
    [super prepareLayout];
    [self.attributesArr removeAllObjects];
    CGFloat itemWidth = self.collectionView.bounds.size.width / _numberOfColumns;
    [self.frameArr removeAllObjects];
    // 通过for循环,在数组中存放每一列总的frame,由于开始的时候不知道frame的高度,所以height给0
    for (int i = 0; i < _numberOfColumns; i++) {
        CGRect frame = CGRectMake(itemWidth * i, 0, itemWidth, 0);
        NSValue *frameValue = [NSValue valueWithCGRect:frame];
        [self.frameArr addObject:frameValue];
    }
    // 确定每一个item存放的位置
    for (int i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        CGSize itemSize = [_delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath];
#pragma height = itemSize.height * itemWidth / itemSize.width;
        CGFloat height = itemSize.height;
        // 获取所有列里面的最小frame
        NSUInteger index = 0;
        CGRect frame = [self minimunFrameOfIndex:&index];
        CGFloat x = frame.origin.x;
        CGFloat y = frame.size.height;
        CGRect itemFrame = CGRectMake(x, y, itemWidth, height);
        // 将item的相关信息例如itemFrame,indexPath封装成一个UICollectionViewLayoutAttributes对象
        UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        attributes.frame = itemFrame;
        [self.attributesArr addObject:attributes];
        // 更新数组(由于新添加了item,所以需要修改数组对应元素)
        CGRect newFrame = CGRectMake(frame.origin.x, 0, itemWidth, frame.size.height + height);
        NSValue *newFrameValue = [NSValue valueWithCGRect:newFrame];
        [self.frameArr replaceObjectAtIndex:index withObject:newFrameValue];
    }
}


 //返回collectionView可滑动的区域
- (CGSize)collectionViewContentSize {
    NSLog(@"================%lf", [self maximumHeight]);
    return CGSizeMake(self.collectionView.bounds.size.width, [self maximumHeight]);
    
//    return CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT- 150);
}

// 返回所有item的layoutAttributes
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
    
    return self.attributesArr;
}

// 返回所有列里面最小的frame.用于计算下一个item存放的位置的x和y.
- (CGRect)minimunFrameOfIndex:(NSUInteger *)index {
    
    CGRect miniFrame = CGRectMake(0, 0, 0, NSIntegerMax);
    for (int i = 0; i < self.frameArr.count; i++) {
        NSValue *frameValue = self.frameArr[i];
        CGRect frame = [frameValue CGRectValue];
        if (CGRectGetHeight(miniFrame) > CGRectGetHeight(frame)) {
            miniFrame = frame;
            *index = i;
        }
    }
    return miniFrame;
}

// 返回数组里面frame最高的高度
- (CGFloat)maximumHeight
{
    CGFloat height = NSIntegerMin;
    for (int i = 0; i < self.frameArr.count; i++) {
        NSValue *frameValue = self.frameArr[i];
        CGRect frame = [frameValue CGRectValue];
        if (CGRectGetHeight(frame) > height) {
            height = CGRectGetHeight(frame);
        }
    }
    return height;
}

@end
